package com.example.demolearn_2;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentTransaction;

import android.os.Bundle;
import android.widget.FrameLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.example.demolearn_2.base.BaseFragment;
import com.example.demolearn_2.fragement.CommonFrameFragment;
import com.example.demolearn_2.fragement.CustomFragment;
import com.example.demolearn_2.fragement.MyFragment;
import com.example.demolearn_2.fragement.OtherFragment;
import com.example.demolearn_2.fragement.ThirdPartyFragment;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends FragmentActivity {
    private FrameLayout mFl_content;
    private RadioGroup mRg_main;
    private RadioButton mRb_common_frame;
    private RadioButton mRb_thirdic_tab_videoparty;
    private RadioButton mRb_custom;
    private RadioButton mRb_other;


    private List<BaseFragment> mBaseList;
    private int position;
    private Fragment mContext;
    private TextView mTv_title;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setBackgroundDrawable(null);
        setContentView(R.layout.activity_main);

        //初始化View
        initView();
        //初始化Fragment
        initFragment();
        //设置RadioGroup的监听
        setListener();
    }


    private void setListener() {
        mRg_main.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId){
                    case R.id.rb_common_frame:
                        position = 0;
                        mTv_title.setText("常用框架");
                        break;
                    case R.id.rb_thirdic_tab_videoparty:
                        position = 1;
                        mTv_title.setText("第三方");
                        break;
                    case R.id.rb_custom:
                        position = 2;
                        mTv_title.setText("自定义控件");
                        break;
                    case R.id.rb_other:
                        position = 3;
                        mTv_title.setText("其他");
                        break;
                    case R.id.rb_my:
                        position = 4;
                        mTv_title.setText("我的");
                        break;
                    default:
                        position = 0;
                        break;
                }

                BaseFragment to = getFragment();
                switchFragment(mContext,to);
            }
        });
        mRg_main.check(R.id.rb_common_frame);
    }

    private void switchFragment(Fragment from, Fragment to) {
        if(from != to){
            mContext = to;
            FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
            if(!to.isAdded()){
                if(from != null){
                    ft.hide(from);
                }
                if(to != null ){
                    ft.add(R.id.fl_content,to).commit();
                }
            }else {
                if(from != null){
                    ft.hide(from);
                }
                if (to != null){
                    ft.show(to).commit();
                }
            }
        }
    }

    private BaseFragment getFragment() {
        return mBaseList.get(position);
    }

    private void initFragment() {
        mBaseList = new ArrayList<>();
        mBaseList.add(new CommonFrameFragment());
        mBaseList.add(new ThirdPartyFragment());
        mBaseList.add(new CustomFragment());
        mBaseList.add(new OtherFragment());
        mBaseList.add(new MyFragment());
    }

    private void initView() {
        mFl_content = (FrameLayout) findViewById(R.id.fl_content);
        mRg_main = (RadioGroup) findViewById(R.id.rg_main);
        mRb_common_frame = (RadioButton) findViewById(R.id.rb_common_frame);
        mRb_thirdic_tab_videoparty = (RadioButton) findViewById(R.id.rb_thirdic_tab_videoparty);
        mRb_custom = (RadioButton) findViewById(R.id.rb_custom);
        mRb_other = (RadioButton) findViewById(R.id.rb_other);
        mTv_title = (TextView)findViewById(R.id.tv_title);
    }
}